package com.citywy.jdbc;

import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcDemo1 {
    static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
    static final String DB_URL="jdbc:mysql://localhost/spring";
    static final String USER="root";
    static final String PASS="123456";

    public static void main(String[] args) throws Exception {
        Connection connection;
        //1.注册驱动(使用Class.forName更好，降低耦合)
        Class.forName(JDBC_DRIVER);
//        DriverManager.registerDriver(new Driver());
        //2.获取连接
        connection = DriverManager.getConnection(DB_URL,USER,PASS);
        //3.获取操作数据库的预处理对象
        String sql="select * from account";
        PreparedStatement pstmt = connection.prepareStatement(sql);
        //4.执行sql，得到结果集
        ResultSet rs = pstmt.executeQuery();
        //5.遍历结果集
        while (rs.next()){
            System.out.println(rs.getInt("id")+"--"+rs.getString("name")+"--"+rs.getDouble("money"));
        }
        //6.释放资源
        rs.close();
        pstmt.close();
        connection.close();
    }
}
